<?php

class ControllerLotteryLottery extends Controller
{

    private $error = array();


    public function index()
    {

       $this->load->language('lottery/lottery');

		$this->document->setTitle('抽奖活动');

		$this->load->model('lottery/lottery');

		$this->getLotteryList();
    }


    //奖品
    public function rewards(){
        $this->load->language('lottery/lottery');

        $this->document->setTitle('活动礼品');

        $this->load->model('lottery/lottery');

        $this->getRewardList();

    }

    public function addlottery(){
        $this->document->setTitle('抽奖活动');

        $this->load->model('lottery/lottery');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {

            if( $this->validateLotteryForm()){

                $this->model_lottery_lottery->addLottery($this->request->post);

                $this->session->data['success'] = "新增成功";

                $this->response->redirect($this->url->link('lottery/lottery', 'token=' . $this->session->data['token'] , 'SSL'));
            }
        }

        $this->getLotteryForm();

    }


    public function addreward(){
        $this->document->setTitle('礼品');

        $this->load->model('lottery/lottery');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {

            if( $this->validateRewardForm()){
                $this->model_lottery_lottery->addLotteryReward($this->request->post);

                $this->session->data['success'] = "新增成功";

                $this->response->redirect($this->url->link('lottery/lottery/rewards', 'token=' . $this->session->data['token'] , 'SSL'));
            }
        }

        $this->getRewardForm();

    }

    protected function validateLotteryForm() {
        if (!$this->user->hasPermission('modify', 'lottery/lottery')) {
            $this->error['warning'] = '无操作权限';
        }

        if ($this->error && !isset($this->error['warning'])) {
            $this->error['warning'] = $this->language->get('error_warning');
        }


        if (utf8_strlen($this->request->post['lottery_name']) < 1 ) {
            $this->error['name'] = "请填写抽奖活动名字";
        }
        return !$this->error;
    }


    protected function validateRewardForm() {
        if (!$this->user->hasPermission('modify', 'lottery/lottery')) {
            $this->error['warning'] = '无操作权限';
        }

        if ($this->error && !isset($this->error['warning'])) {
            $this->error['warning'] = $this->language->get('error_warning');
        }


        if (utf8_strlen($this->request->post['name']) < 1 ) {
            $this->error['name'] = "请礼品名字";
        }



        return !$this->error;
    }




    public function editlottery(){

        $this->document->setTitle('抽奖活动');

        $this->load->model('lottery/lottery');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {

            if( $this->validateLotteryForm()){

                $this->model_lottery_lottery->editLottery($this->request->get['lottery_id'],$this->request->post);

                $this->session->data['success'] = "编辑成功";

                $this->response->redirect($this->url->link('lottery/lottery', 'token=' . $this->session->data['token'] , 'SSL'));
            }
        }

        $this->getLotteryForm();
    }


    public function editreward(){

        $this->document->setTitle('礼品');

        $this->load->model('lottery/lottery');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {

            if( $this->validateRewardForm()){


                $this->model_lottery_lottery->editLotteryReward($this->request->get['lottery_reward_id'],$this->request->post);


                $this->session->data['success'] = "编辑成功";

                $this->response->redirect($this->url->link('lottery/lottery/rewards', 'token=' . $this->session->data['token'] , 'SSL'));
            }
        }

        $this->getRewardForm();
    }

    public function deletelottery(){

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('lottery/lottery');

        if (isset($this->request->post['selected'])) {
            foreach ($this->request->post['selected'] as $lottery_id) {
                $this->model_lottery_lottery->deleteLottery($lottery_id);
            }

            $this->session->data['success'] = "删除成功";

            $url = '';


            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->response->redirect($this->url->link('lottery/lottery', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getLotteryList();

    }

    public function deletereward(){

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('lottery/lottery');

        if (isset($this->request->post['selected'])) {
            foreach ($this->request->post['selected'] as $lottery_reward_id) {
                $this->model_lottery_lottery->deleteLotteryReward($lottery_reward_id);
            }

            $this->session->data['success'] = "删除成功";

            $url = '';


            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->response->redirect($this->url->link('lottery/lottery/rewards', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getRewardList();

    }





    public function add()
    {
        $this->load->language('sale/order');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('sale/order');

        unset($this->session->data['cookie']);

        if ($this->validate()) {
            // API
            $this->load->model('user/api');

            $api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));

            if ($api_info) {
                $curl = curl_init();

                // Set SSL if required
                if (substr(HTTPS_CATALOG, 0, 5) == 'https') {
                    curl_setopt($curl, CURLOPT_PORT, 443);
                }

                curl_setopt($curl, CURLOPT_HEADER, false);
                curl_setopt($curl, CURLINFO_HEADER_OUT, true);
                curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_URL, HTTPS_CATALOG . 'index.php?route=api/login');
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($api_info));

                $json = curl_exec($curl);

                if (! $json) {
                    $this->error['warning'] = sprintf($this->language->get('error_curl'), curl_error($curl), curl_errno($curl));
                } else {
                    $response = json_decode($json, true);

                    if (isset($response['cookie'])) {
                        $this->session->data['cookie'] = $response['cookie'];
                    }

                    curl_close($curl);
                }
            }
        }

        $this->getForm();
    }

    public function edit()
    {
        $this->load->language('sale/order');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('sale/order');

        unset($this->session->data['cookie']);

        if ($this->validate()) {
            // API
            $this->load->model('user/api');

            $api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));

            if ($api_info) {
                $curl = curl_init();

                // Set SSL if required
                if (substr(HTTPS_CATALOG, 0, 5) == 'https') {
                    curl_setopt($curl, CURLOPT_PORT, 443);
                }

                curl_setopt($curl, CURLOPT_HEADER, false);
                curl_setopt($curl, CURLINFO_HEADER_OUT, true);
                curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_URL, HTTPS_CATALOG . 'index.php?route=api/login');
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($api_info));

                $json = curl_exec($curl);

                if (! $json) {
                    $this->error['warning'] = sprintf($this->language->get('error_curl'), curl_error($curl), curl_errno($curl));
                } else {
                    $response = json_decode($json, true);

                    if (isset($response['cookie'])) {
                        $this->session->data['cookie'] = $response['cookie'];
                    }

                    curl_close($curl);
                }
            }
        }

        $this->getForm();
    }

    public function delete()
    {
        $this->load->language('sale/order');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('sale/order');

        unset($this->session->data['cookie']);

        if (isset($this->request->get['order_id']) && $this->validate()) {
            // API
            $this->load->model('user/api');

            $api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));

            if ($api_info) {
                $curl = curl_init();

                // Set SSL if required
                if (substr(HTTPS_CATALOG, 0, 5) == 'https') {
                    curl_setopt($curl, CURLOPT_PORT, 443);
                }

                curl_setopt($curl, CURLOPT_HEADER, false);
                curl_setopt($curl, CURLINFO_HEADER_OUT, true);
                curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_URL, HTTPS_CATALOG . 'index.php?route=api/login');
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($api_info));

                $json = curl_exec($curl);

                if (! $json) {
                    $this->error['warning'] = sprintf($this->language->get('error_curl'), curl_error($curl), curl_errno($curl));
                } else {
                    $response = json_decode($json, true);

                    if (isset($response['cookie'])) {
                        $this->session->data['cookie'] = $response['cookie'];
                    }

                    curl_close($curl);
                }
            }
        }

        if (isset($this->session->data['cookie'])) {
            $curl = curl_init();

            // Set SSL if required
            if (substr(HTTPS_CATALOG, 0, 5) == 'https') {
                curl_setopt($curl, CURLOPT_PORT, 443);
            }

            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLINFO_HEADER_OUT, true);
            curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_URL, HTTPS_CATALOG . 'index.php?route=api/order/delete&order_id=' . $this->request->get['order_id']);
            curl_setopt($curl, CURLOPT_COOKIE, session_name() . '=' . $this->session->data['cookie'] . ';');

            $json = curl_exec($curl);

            if (! $json) {
                $this->error['warning'] = sprintf($this->language->get('error_curl'), curl_error($curl), curl_errno($curl));
            } else {
                $response = json_decode($json, true);

                curl_close($curl);

                if (isset($response['error'])) {
                    $this->error['warning'] = $response['error'];
                }
            }
        }

        if (isset($response['error'])) {
            $this->error['warning'] = $response['error'];
        }

        if (isset($response['success'])) {
            $this->session->data['success'] = $response['success'];

            $url = '';

            if (isset($this->request->get['filter_order_id'])) {
                $url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
            }

            if (isset($this->request->get['filter_customer'])) {
                $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
            }

            if (isset($this->request->get['filter_order_status'])) {
                $url .= '&filter_order_status=' . $this->request->get['filter_order_status'];
            }

            if (isset($this->request->get['filter_total'])) {
                $url .= '&filter_total=' . $this->request->get['filter_total'];
            }

            if (isset($this->request->get['filter_date_added'])) {
                $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
            }

            if (isset($this->request->get['filter_date_modified'])) {
                $url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
            }

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->response->redirect($this->url->link('sale/order', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getList();
    }

    protected function getLotteryList()
    {
        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $url = '';



        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }


        $data['add'] = $this->url->link('lottery/lottery/addlottery', 'token=' . $this->session->data['token'], 'SSL');

        $data['delete'] = $this->url->link('lottery/lottery/deletelottery', 'token=' . $this->session->data['token'] . $url, 'SSL');

        $data['lotterys'] = array();

        $filter_data = array(
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
            'limit' => $this->config->get('config_limit_admin')
        );



        $lottery_total = $this->model_lottery_lottery->getTotalLotteries($filter_data);

        $results = $this->model_lottery_lottery->getLotteries($filter_data);

        foreach ($results as $result) {
            $data['lotterys'][] = array(
                'lottery_id' => $result['lottery_id'],
                'name' => $result['name'],
                'date_added' => $result['date_added'],
                'edit' => $this->url->link('lottery/lottery/editlottery', 'token=' . $this->session->data['token'] . '&lottery_id=' . $result['lottery_id'] . $url, 'SSL')
            );
        }

        $data['text_list'] = $this->language->get('text_list');
        $data['text_no_results'] = $this->language->get('text_no_results');
        $data['text_confirm'] = $this->language->get('text_confirm');
        $data['text_missing'] = $this->language->get('text_missing');


        $data['button_add'] = $this->language->get('button_add');
        $data['button_edit'] = $this->language->get('button_edit');
        $data['button_delete'] = $this->language->get('button_delete');
        $data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $data['success'] = '';
        }

        if (isset($this->request->post['selected'])) {
            $data['selected'] = (array) $this->request->post['selected'];
        } else {
            $data['selected'] = array();
        }



        $pagination = new Pagination();
        $pagination->total = $lottery_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_limit_admin');
        $pagination->url = $this->url->link('lottery/lottery', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

        $data['pagination'] = $pagination->render();

        $data['results'] = sprintf($this->language->get('text_pagination'), ($lottery_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($lottery_total - $this->config->get('config_limit_admin'))) ? $lottery_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $lottery_total, ceil($lottery_total / $this->config->get('config_limit_admin')));

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        $this->response->setOutput($this->load->view('lottery/lottery_list.tpl', $data));
    }


    protected function getRewardList(){
        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $url = '';



        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }
        $data = array();

        $data['add'] = $this->url->link('lottery/lottery/addreward', 'token=' . $this->session->data['token'], 'SSL');

        $data['delete'] = $this->url->link('lottery/lottery/deletereward', 'token=' . $this->session->data['token'] . $url, 'SSL');

        $data['rewards'] = array();

        $filter_data = array(
            'start' => ($page - 1) * $this->config->get('config_limit_admin'),
            'limit' => $this->config->get('config_limit_admin')
        );



        $rewards_total = $this->model_lottery_lottery->getTotalLotteryRewards($filter_data);

        $results = $this->model_lottery_lottery->getLotteryRewards($filter_data);

        foreach ($results as $result) {
            $data['rewards'][] = array(
                'lottery_reward_id' => $result['lottery_reward_id'],
                'name' => $result['name'],
                'link' => $result['link'],
                'lottery_name'=>$result['lottery_name'],
                'date_added' => $result['date_added'],
                'stock_num'=>$result['stock_num'],
                'points'=>$result['points'],
                'lottery_id'=>$result['lottery_id'],
                'type'=>$result['type'], //ey 1:积分 2:优惠券
                'edit' => $this->url->link('lottery/lottery/editreward', 'token=' . $this->session->data['token'] . '&lottery_reward_id=' . $result['lottery_reward_id'] . $url, 'SSL')
            );
        }

        $data['text_list'] = '礼品列表';
        $data['text_no_results'] = $this->language->get('text_no_results');
        $data['text_confirm'] = $this->language->get('text_confirm');
        $data['text_missing'] = $this->language->get('text_missing');


        $data['button_add'] = '添加';
        $data['button_edit'] = '编辑';
        $data['button_delete'] = '删除';
        $data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $data['success'] = '';
        }

        if (isset($this->request->post['selected'])) {
            $data['selected'] = (array) $this->request->post['selected'];
        } else {
            $data['selected'] = array();
        }



        $pagination = new Pagination();
        $pagination->total = $rewards_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_limit_admin');
        $pagination->url = $this->url->link('lottery/lottery/rewards', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

        $data['pagination'] = $pagination->render();

        $data['results'] = sprintf($this->language->get('text_pagination'), ($rewards_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($rewards_total - $this->config->get('config_limit_admin'))) ? $rewards_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $rewards_total, ceil($rewards_total / $this->config->get('config_limit_admin')));

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        $this->response->setOutput($this->load->view('lottery/lottery_reward_list.tpl', $data));
    }

    public function getLotteryForm()
    {
        $this->load->model('lottery/lottery');

        $data['heading_title'] = '抽奖活动';

        $data['text_form'] = ! isset($this->request->get['lottery_id']) ? '添加抽奖活动' : '编辑抽奖活动';


        $data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		}

		if (isset($this->error['name'])) {
			$data['error_name'] = $this->error['name'];
		}


        $url = '';

        if (isset($this->request->get['lottery_id'])) {
            $url .= '&lottery_id=' . $this->request->get['lottery_id'];
        }

        $data['cancel'] = $this->url->link('lottery/lottery', 'token=' . $this->session->data['token'] . $url, 'SSL');

        if (isset($this->request->get['lottery_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $lottery_info = $this->model_lottery_lottery->getLottery($this->request->get['lottery_id']);
        }

        if (! empty($lottery_info)) {
            $data['lottery_id'] = $this->request->get['lottery_id'];
            $data['lottery_name'] = $lottery_info['name'];
        } else {
            $data['lottery_id'] = 0;
            $data['lottery_name'] = '';
        }

        if (!isset($this->request->get['lottery_id'])) {
            $data['action'] = $this->url->link('lottery/lottery/addlottery', 'token=' . $this->session->data['token'] . $url, 'SSL');
        } else {
            $data['action'] = $this->url->link('lottery/lottery/editlottery', 'token=' . $this->session->data['token'] . '&lottery_id=' . $this->request->get['lottery_id'] . $url, 'SSL');
        }

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('lottery/lottery_form.tpl', $data));
    }


    public function getRewardForm()
    {
        $this->load->model('lottery/lottery');

        $data = array();

        $data['heading_title'] = '礼品';

        $data['text_form'] = ! isset($this->request->get['lottery_reward_id']) ? '添加礼品' : '编辑礼品';


        $data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        }

        if (isset($this->error['name'])) {
            $data['error_name'] = $this->error['name'];
        }


        $url = '';

        if (isset($this->request->get['lottery_reward_id'])) {
            $url .= '&lottery_reward_id=' . $this->request->get['lottery_reward_id'];
        }

        $data['cancel'] = $this->url->link('lottery/lottery/rewards', 'token=' . $this->session->data['token'] . $url, 'SSL');

        if (isset($this->request->get['lottery_reward_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $reward_info = $this->model_lottery_lottery->getLotteryReward($this->request->get['lottery_reward_id']);
        }

         $data['types']=array(
            array(
               'value'=>1,
                'text'=>'积分'
            ),
             array(
                 'value'=>2,
                 'text'=>'优惠券'
             )
         );//ey 1:积分 2:优惠券


        $data['lotterys'] =$this->model_lottery_lottery->getAllLotteries();
        if (! empty($reward_info)) {
            $data['lottery_reward_id'] = $this->request->get['lottery_reward_id'];
            $data['lottery_id']=$reward_info['lottery_id'];
            $data['name'] = $reward_info['name'];
            $data['type'] = $reward_info['type'];
            $data['points'] = $reward_info['points'];
            $data['link'] = $reward_info['link'];
            $data['stock_num'] = $reward_info['stock_num'];
            $data['date_added'] = $reward_info['date_added'];
            $data['lottery_id']= $reward_info['lottery_id'];
        } else {
            $data['lottery_reward_id']=0;
            $data['lottery_id']=0;
            $data['name'] = '';
            $data['type'] = 1;
            $data['points'] = 0;
            $data['link'] = '';
            $data['stock_num'] = 0;
            $data['date_added'] = '';
            $data['lottery_id']= 0;
        }

        if (!isset($this->request->get['lottery_reward_id'])) {
            $data['action'] = $this->url->link('lottery/lottery/addreward', 'token=' . $this->session->data['token'] . $url, 'SSL');
        } else {
            $data['action'] = $this->url->link('lottery/lottery/editreward', 'token=' . $this->session->data['token'] . '&lottery_reward_id=' . $this->request->get['lottery_reward_id'] . $url, 'SSL');
        }

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('lottery/lottery_reward_form.tpl', $data));
    }




    public function api()
    {
        $this->load->language('sale/order');

        if ($this->validate()) {
            // Store
            if (isset($this->request->get['store_id'])) {
                $store_id = $this->request->get['store_id'];
            } else {
                $store_id = 0;
            }

            $this->load->model('setting/store');

            $store_info = $this->model_setting_store->getStore($store_id);

            if ($store_info) {
                $url = $store_info['ssl'];
            } else {
                $url = HTTPS_CATALOG;
            }

            if (isset($this->session->data['cookie']) && isset($this->request->get['api'])) {
                // Include any URL perameters
                $url_data = array();

                foreach ($this->request->get as $key => $value) {
                    if ($key != 'route' && $key != 'token' && $key != 'store_id') {
                        $url_data[$key] = $value;
                    }
                }

                $curl = curl_init();

                // Set SSL if required
                if (substr($url, 0, 5) == 'https') {
                    curl_setopt($curl, CURLOPT_PORT, 443);
                }
                curl_setopt($curl, CURLOPT_HEADER, false);
                curl_setopt($curl, CURLINFO_HEADER_OUT, true);
                curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

                curl_setopt($curl, CURLOPT_URL, $url . 'index.php?route=' . $this->request->get['api'] . ($url_data ? '&' . http_build_query($url_data) : ''));

                if ($this->request->post) {
                    curl_setopt($curl, CURLOPT_POST, true);
                    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->request->post));
                }

                curl_setopt($curl, CURLOPT_COOKIE, session_name() . '=' . $this->session->data['cookie'] . ';');

                $json = curl_exec($curl);

                curl_close($curl);
            }
        } else {

            $response = array();

            $response['error'] = $this->error;

            $json = json_encode($response);
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput($json);
    }




}